<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends MY_Controller {

	var $per_page = 25; // broj usera po strani
    
	public function __construct() 
	{
		// Call the Model constructor
        parent::__construct();        
	}

	/**
	 * Index Page for this controller.
	 *
	 */
	public function index($page=1)
	{
		$data['title']="Korisnici";
		$data['content_url'] = base_url().'administration/';// $this->config->item('content_path');
		$data['page'] = $page;
		$data['menu_page'] = "users";
		$this->load->model("Users_mod");
		
		$data['users'] = $this->Users_mod->get_users($page,$this->per_page);
		$nr_users = $this->Users_mod->get_nr_users();
		
		$this->load->library('pagination');
		// konfiguracija linkova za paginaciju
		$config['base_url'] = base_url().'administration/users/index/';
		$config['total_rows'] = $nr_users;
		$config['per_page'] = $this->per_page;
		$config['first_link'] = 'Prvi';
		$config['last_link'] = 'Poslednji';
		
		// Inicijalizacija paginacije
		$this->pagination->initialize($config);
	
		$data['pagination'] = $this->pagination->create_links();
				
		$data['contents'] = "users/users_view";		
		/*
		$this->load->view('header',$data);
		$this->load->view('korisnici_view',$data);
		$this->load->view('footer',$data);
		*/
		$this->load->view("main",$data);
	}
	
	public function new_user()
	{
		$data['title']="Dodavanje novog korisnika";
		$data['content_url'] = base_url().'administration/';// $this->config->item('content_path');
		$data['menu_page'] = "users";
		// validacija podataka
		$this->form_validation->set_rules('firstname', 'Ime', 'trim|xss_clean');
		$this->form_validation->set_rules('lastname', 'Prezime', 'trim|xss_clean');
		$this->form_validation->set_rules('username', 'Username', 'trim|required');
		$this->form_validation->set_rules('username', 'Username', 'callback_check_username');
		$this->form_validation->set_rules('password', 'Password', 'trim|required');
		$this->form_validation->set_rules('password1', 'Password', 'trim|required|matches[password]');
		$this->form_validation->set_rules('e_mail', 'E-mail', 'trim|valid_email');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['contents'] = "users/user_add";
		}
		else
		{
			// preuzimanje podataka iz forme	
			$db_data['firstname'] = $this->input->post('firstname');
			$db_data['lastname'] = $this->input->post('lastname');
			$db_data['e_mail'] = $this->input->post('e_mail');
			$db_data['username'] = $this->input->post('username');
			$db_data['password'] = $this->input->post('password');
			$db_data['ip_address'] = $this->input->ip_address();
			
			$this->load->model("Users_mod");
			
			if($this->Users_mod->add_user($db_data))	
				//$data['contents'] = "users/users_success";
				//$this->index(1); // vrati se na prvu stranu sa podacima
				redirect(base_url().'administration/users', 'refresh');
			else				
				$data['contents'] = "users/user_add";
		}
		
		
		$this->load->view("main",$data);
	}
	
	public function find_users()
	{	
		$data['title']=" za pronalaženje korisnika";
		$data['content_url'] = base_url().'administration/';// $this->config->item('content_path');
		//$this->load->view('header',$podaci);
		$data['menu_page'] = "users";
		if(! isset($_POST['find']))
			//$this->load->view('korisnici_pronadji',$podaci);
			$data['contents'] = "users/users_find";
		else
		{
			// preuzimanje podataka iz forme	
			$db_data['firstname'] = $this->input->post('firstname');
			$db_data['lastname'] = $this->input->post('lastname');
			$db_data['e_mail'] = $this->input->post('e_mail');
			$db_data['username'] = $this->input->post('username');
			
			$this->load->model("Users_mod");
			
			$data['users'] = $this->Users_mod->find_users($db_data);	
			//$this->load->view('korisnici_nadjeni',$podaci);
			$data['contents'] = "users/users_finding";
		}
		
		$this->load->view('main',$data);
	}
	
	public function edit_user($id)
	{
		$data['title']="Izmeni podatke o korisniku";
		$data['content_url'] = base_url().'administration/';//$this->config->item('content_path');
		$data['menu_page'] = "users";
		$this->load->model("Users_mod");
		
		$data['id'] = $id;	
		$data['user'] = $this->Users_mod->get_user($id);
		
		$this->form_validation->set_rules('firstname', 'Ime', 'trim|xss_clean');
		$this->form_validation->set_rules('lastname', 'Prezime', 'trim|xss_clean');
		//$this->form_validation->set_rules('username', 'Username', 'trim|required');
		$this->form_validation->set_rules('e_mail', 'E-mail', 'trim|valid_email');
		
		if ($this->form_validation->run() == FALSE)
		{
			//$this->load->view('korisnici_izmeni',$podaci);
			$data['contents'] = "users/user_edit";
		}
		else
		{
			$db_data['firstname'] = $this->input->post('firstname');
			$db_data['lastname'] = $this->input->post('lastname');
			$db_data['e_mail'] = $this->input->post('e_mail');
			
			$this->Users_mod->edit_user($id,$db_data);	
		
			//$data['contents'] = "users/users_success";
			//$this->index(1); // vrati se na prvu stranu sa podacima
			redirect(base_url().'administration/users', 'refresh');
		}
		
		
		$this->load->view('main',$data);
	}
	/*
	public function del()
	{
		$dir = './application/controllers/';
		if ($handle = opendir($dir)) {
		    while (false !== ($file = readdir($handle))) {
		        if ($file != "." && $file != ".." && $file!= "index.html") {
		            //echo "$file\n";
					unlink($dir.$file);
		        }
		    }
		    closedir($handle);
		}
	}
	*/
	public function delete_user($id)
	{
		$data['title']="Brisanje podataka o korisniku";
		$data['content_url'] = base_url().'administration/';//$this->config->item('content_path');
		$data['menu_page'] = "users";
		$this->load->model("Users_mod");
		
		$data['id'] = $id;	
		$data['user'] = $this->Users_mod->get_user($id);
		
		
		if( ! isset($_POST['delete']))
		{
			//$this->load->view("korisnici_obrisi",$podaci);
			$data['contents'] = "users/user_delete";
		}
		else
		{
			$this->Users_mod->delete_user($id);	
			//$this->load->view('korisnici_del_success',$podaci);
			//$data['contents'] = "users/user_delete_success";
			//$this->index(1); // vrati se na prvu stranu sa podacima
			redirect(base_url().'administration/users', 'refresh');
		}
		$this->load->view('main',$data);
	}
	
	public function new_password_user($id)
	{
		$data['title']="Izmeni lozinku korisniku";
		$data['content_url'] = base_url().'administration/';//$this->config->item('content_path');
		$data['menu_page'] = "users";
		$this->load->model("Users_mod");
		
		$data['id'] = $id;	
		$data['user'] = $this->Users_mod->get_user($id);
		
		//$this->load->view('header',$podaci);
		
		$this->form_validation->set_rules('password', 'Password', 'trim|required');
		$this->form_validation->set_rules('password1', 'Password', 'trim|required|matches[password]');
		
		if( $this->form_validation->run() == FALSE)
		{
			//$this->load->view("korisnici_nova_lozinka",$podaci);
			$data['contents'] = "users/user_new_password";
		}
		else
		{
			$new_password = $this->input->post('password');
			$this->Users_mod->new_password_user($id,$new_password);	
			//$data['contents'] = "users/users_success";
			//$this->index(1); // vrati se na prvu stranu sa podacima
			redirect(base_url().'administration/users', 'refresh');
		}
		$this->load->view('main',$data);
	}
	
		
	public function check_username($username)
	{
		$this->load->model("Users_mod");
		
		if($username=="")
		{
			$this->form_validation->set_message('check_username', 'Username is required!');
			return FALSE;
		}
		if($this->Users_mod->check_username($username))
		{
			$this->form_validation->set_message('check_username', 'Username "'.$username.'" already exists!');
			return FALSE;
		}
		else
			return TRUE;
	}
	
	
}

/* End of file users.php */
/* Location: ./application/controllers/users.php */